﻿using Digitalmes.Core.Common.SerialPorts;

namespace Digitalmes.Infrastructure.IO.SerialPorts;

/// <summary>
/// 串口管理对象。
/// </summary>
internal sealed class SerialPortsManager(ISerialPortsProvider serialPortsProvider) : ISerialPortsManager
{
    /// <summary>
    /// 获取所有注册的串口对象。
    /// </summary>
    public IReadOnlyList<IRsSerialPort> SerialPorts => serialPortsProvider.SerialPorts;

    /// <summary>
    /// 重新加载配置的串口。
    /// </summary>
    public async Task ReloadAsync(CancellationToken cancellationToken = default)
    {
        serialPortsProvider.Clear();
        await serialPortsProvider.LoadAsync(cancellationToken);
    }

    public void Dispose()
    {
        serialPortsProvider.Clear();
    }
}
